--[[
描述 ： 弹出提示框
作者 ： gengdy
时间 ： 14/04/14
]]

UIAlertView = Class(MLView,'UIAlertView');

UIAlertView.onClickEvent  = nil;    -- 点击按钮回调事件
UIAlertView.mWidth      = nil;      -- 宽度
UIAlertView.mTitleHeight  = nil;    -- 标题高度
UIAlertView.mHeight     = nil;      -- 内容高度
UIAlertView.mLR_GAP     = nil;    -- 距离左右间距

UIAlertView.mTitle      = nil;    -- 标题
UIAlertView.mTitleFont    = nil;    -- 标题字体大小
UIAlertView.mContext    = nil;    -- 内容
UIAlertView.mContexfFont  = nil;    -- 内容字体大小
UIAlertView.mCannelTxt    = nil;    -- 左按钮文字
UIAlertView.mOtherTxt     = nil;    -- 又按钮文字
UIAlertView.mIsOneBtn     = nil;    -- 是否一个按钮

UIAlertView.mPopCanvas    = nil;
UIAlertView.mPopCover     = nil;
UIAlertView.mViewCtrl     = nil;


--[[
功能   ： 初始化 UIAlertView 控件
参数   ：
title      --标题
context    --内容
canneltxt  --取消按钮标题（左）
othertxt   --其他按钮标题（右）
返回   ： UIAlertView 实例
]]

function UIAlertView:init(title, context, canneltxt, othertxt,viewCtrl,id)
  if not viewCtrl then print('Warning:UIAlertView参数viewCtrl为nil'); return end
  --local deviceW, deviceH = MLNative.getScreen():getWH();
  local deviceW, deviceH = viewCtrl.rootView:getWH();
  -- 初始化
  self:initView(AW(UITheme.UIALERTVIEW_WIDTH), AW(UITheme.UIALERTVIEW_TITLE_HEIGHT)+AW(UITheme.UIALERTVIEW_CONTEXT_HEIGHT),viewCtrl,id);

--  self.mViewCtrl = {};
  --setmetatable(self.mViewCtrl, {__mode="kv"});   -- 弱引用
   self.weekTable.mViewCtrl = viewCtrl;
  
  self.mWidth = AW(UITheme.UIALERTVIEW_WIDTH);
  self.mTitleHeight = AW(UITheme.UIALERTVIEW_TITLE_HEIGHT);
  self.mHeight = AW(UITheme.UIALERTVIEW_CONTEXT_HEIGHT);
  self.mLR_GAP = AW(UITheme.UIALERTVIEW_LR_GAP);

  self.fs = MLNative.Style_Italic;
  self.mTitleFont =  AW(UITheme.UIALERTVIEW_TITLE_FONT);
  self.mContexfFont = AW(UITheme.UIALERTVIEW_CONTEXT_FONT);

  self.mTitle = title or '';
  self.mContext = context or '';
  self.mCannelTxt = canneltxt;
  self.mOtherTxt = othertxt;

  self.mIsOneBtn = false;
  -- print('----', canneltxt, othertxt);
  if (canneltxt == nil or string.len(canneltxt) == 0) then
    self.mIsOneBtn = true;
    self.mCannelTxt = self.mOtherTxt;
  end

  if (othertxt == nil or string.len(othertxt) == 0) then
    if self.mIsOneBtn then
      self.mCannelTxt = '确定';
    end
    self.mIsOneBtn = true;
  end

  self:draw();
  self:PopView(self);
  return self;
end

--[[
功能   ： 画控件
参数   ： 无
返回   ： 无
]]
function  UIAlertView:draw()
  -- if not self.text then return end
  local w, h = self:getWH();
  local dw, dh = MLNative.deviceWH();

  --判断标题高度
  local titleH = 0;
  if string.len(self.mTitle) ~= 0 then
    -- local th = UIUtils:TextGetHeight(self.mTitle, self.mTitleFont, w-self.mLR_GAP*2, self.fs, nil);
    local th = MLNative.measStringHeight(self.mTitle, self.mTitleFont, self.fs, w-self.mLR_GAP*2);
    titleH = th + AH(15);
  end
  --判断内容高度
  local contextH = AH(40);
  if string.len(self.mContext) ~= 0 then
    -- local th = UIUtils:TextGetHeight(self.mContext, self.mContexfFont, w-self.mLR_GAP*2, self.fs, nil);
    local th = MLNative.measStringHeight(self.mContext, self.mContexfFont, self.fs, w-self.mLR_GAP*2);
    contextH = th + AH(40) + AH(10);
  end

  if titleH + contextH < 100 then
    self:setWH(self.mWidth, 100);
  else
    self:setWH(self.mWidth, titleH+contextH);
  end
  local w, h = self:getWH();

  local g = self:getGraphic();
  g:clear();
  --BG
  g:fillRect(0, 0, w, h, UIColor.COLOR_WHITE, AW(10));

  local lines = UIUtils.calcTextLine(self.mTitle, w-self.mLR_GAP*2, 999, self.mTitleFont, self.fs);
  for i=1, #lines do
    local tw = MLNative.measStringWidth(lines[i], self.mTitleFont, nil);
    local x = (w-tw)/2;
    local y = AH(15) + self.mTitleFont*(i-1);
    g:drawString(lines[i], x, y, UIColor.COLOR_BLACK, self.mTitleFont, self.fs, nil);
  end

  local lines = UIUtils.calcTextLine(self.mContext, w-self.mLR_GAP*2, 999, self.mContexfFont, self.fs);
  for i=1, #lines do
    local tw = MLNative.measStringWidth(lines[i], self.mContexfFont, nil);
    local x = (w-tw)/2;
    local y = titleH+(h-#lines*self.mContexfFont-AH(40)-titleH)/2 + self.mContexfFont*(i-1);
    -- local y = titleH + AH(5) + self.mContexfFont*(i-1);
    g:drawString(lines[i], x, y,  UIColor.COLOR_WEAKBLACK, self.mContexfFont, self.fs, nil);
  end

  g:drawLine(0, h-AH(40), w, h-AH(40), UIColor.COLOR_GRAY, 1);

  if self.mIsOneBtn then
    local btn = UIButton.new():init(w, AH(40),self.weekTable.mViewCtrl);
    btn.text = self.mCannelTxt;
    btn.fontColor = UIColor.COLOR_BLUE;
    btn.bgImage = UIImage.imageName('');
    btn.focusImage = UIImage.imageName('');
    btn.onClick = function ()
      -- body
      if self.onClickEvent then
        self.onClickEvent(1);
        self:ClosePop(self);
      end
    end;
    btn:show();
    self:add(btn, 0, h-AH(40));
  else
    g:drawLine(w/2, h-AH(40), w/2, h, UIColor.COLOR_GRAY, 1);

    local btn = UIButton.new():init(w/2, AH(40),self.weekTable.mViewCtrl);
    btn.text = self.mCannelTxt;
    btn.fontColor = UIColor.COLOR_BLUE;
    btn.bgImage = UIImage.imageName('');
    btn.focusImage = UIImage.imageName('');
    btn.onClick = function ()
      -- body
      self.onClickEvent(1);
      self:ClosePop(self);
    end;
    btn:show();
    self:add(btn, 0, h-AH(40));

    local btn = UIButton.new():init(w/2, AH(40),self.weekTable.mViewCtrl);
    btn.text = self.mOtherTxt;
    btn.fontColor = UIColor.COLOR_BLUE;
    btn.bgImage = UIImage.imageName('');
    btn.focusImage = UIImage.imageName('');
    btn.onClick = function ()
      -- body
      self.onClickEvent(2);
      self:ClosePop(self);
    end;
    btn:show();
    self:add(btn, w/2, h-AH(40));
  end

end

--[[
功能   ： Pop View
参数   ： 无 self
返回   ： 无
]]
function UIAlertView:PopView(view)
  -- print('-----pop-self', self);
  -- local scr = MLNative.getScreen();
  -- if not self.mViewCtrl.navCtrl then
  --   self.mViewCtrl.navCtrl = Window.navCtrl;
  --   end


  --if not self.mViewCtrl.navCtrl then self.mViewCtrl.navCtrl = Window.navCtrl end
  --local scr = self.mViewCtrl.navCtrl.viewCtrls[#self.mViewCtrl.navCtrl.viewCtrls].rootView;
  local scr = self.weekTable.mViewCtrl.rootView;
  local sw, sh = MLNative.deviceWH();
  local w, h = view:getWH();
  if self.mPopCanvas  then return; end

  self.mPopCover = MLView.new():initView(sw, sh,self.weekTable.mViewCtrl);
  -- local g = self.mPopCover:getGraphic();
  -- g:clear();
  -- g:fillRect (0, 0, sw, sh, UIColor.COLOR_GRAY, 0);

  self.mPopCover:setColor(0x000000);
  self.mPopCover:setAlpha(0.65);
  self.mPopCover.onClick = function (  )
  -- body
  end
  scr:add(self.mPopCover, 0, 0);

  scr:add(view, (sw-w)/2, (sh-h)/2);

  self.mPopCanvas = view;
end

--[[
功能   ： Close View
参数   ： 无
返回   ： 无
]]
function UIAlertView:ClosePop(view)
  -- print('---close self', self);
  -- if self.mPopCover then MLNative.getScreen():del(self.mPopCover); end
  -- if self.mPopCanvas then MLNative.getScreen():del(self.mPopCanvas); end
  --if self.mPopCover then self.mViewCtrl.navCtrl.viewCtrls[#self.mViewCtrl.navCtrl.viewCtrls].rootView:del(self.mPopCover); end
  --if self.mPopCanvas then self.mViewCtrl.navCtrl.viewCtrls[#self.mViewCtrl.navCtrl.viewCtrls].rootView:del(self.mPopCanvas); end
  
  if self.mPopCover then self.weekTable.mViewCtrl.rootView:del(self.mPopCover); end
  if self.mPopCanvas then self.weekTable.mViewCtrl.rootView:del(self.mPopCanvas); end
  self.mPopCover = nil;
  self.mPopCanvas = nil;
end

--[[
功能   ： 显示控件
参数   ： 无
返回   ： 无
]]
function UIAlertView:show()
  self:draw();
  self:PopView(self);
end
--[[
功能   ： 更新控件
参数   ： 无
返回   ： 无
]]
function UIAlertView:update()
  self:show();
end



--[[ 

-----使用方法 V2.0

local alert = UIAlertView.new():init('标题', '内容', '左按钮', '右按钮',self);
alert.onClickEvent = function ( v )
    -- v 返回  1-左侧按钮   2- 右侧按钮
    print('alert-btn-click', v);
end
--]]
